// pages/lesson/lesson.js
import {request, requestWrap} from "../../request/index.js"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isShow: false,
        isSearch: false,
        termId: '',
        termName: '',
        termList: [],
        filterList: [],
        dataList: ''
    },
    name: '',
    student: '',
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.student = wx.getStorageSync('student')
        this.getTermList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    openSelect () {
        this.setData({
            filterList: this.data.termList,
            isShow: true
        })
    },
    closeSelect () {
        this.setData({
            isShow: false
        })
    },
    showSearch () {
        this.setData({
            isSearch: true
        })
    },
    closeSearch () {
        this.setData({
            isSearch: false
        })
    },
    onInput (e) {
        // console.log(e)
        let {value} = e.detail
        if (value) {
            let {termList} = this.data
            let filterList = termList.filter(data => {
                if (data.name.toUpperCase().indexOf(value.toUpperCase()) >= 0) {
                    return data
                }
            })
            this.setData({
                filterList
            })
            
        } else {
            this.setData({
                filterList: this.data.termList
            })
        }
    },
    selectTerm (e) {
        this.setData({
            isShow: false,
            isSearch: false,
            termId: e.currentTarget.dataset.id,
            termName: e.currentTarget.dataset.name
        })
    },
    async getCourse () {
        let data = {}
        data.student_id = this.student.id
        if (this.data.termId) {
            data.semester = this.data.termName
        }
        let res = requestWrap(await request({
            url:"api/wxapp/student/studyList",
            method:'GET',
            data
        }))
        if (res && res.code == 1) {
          if (res.data && res.data.length > 0) {
                let dataList = res.data.map(data => {
                if (data.a_status == 0) {
                    data.a_status_txt = '未开课'
                } else if (data.a_status == 1) {
                    data.a_status_txt = '已开课'
                } else if (data.a_status == 2) {
                    data.a_status_txt = '已结束'
                } else if (data.a_status == 3) {
                    data.a_status_txt = '已结算'
                } else {
                    data.a_status_txt = data.a_status
                }
                return data
            })
            console.log(dataList)
            this.setData({
              dataList
            })
          } else {
            this.setData({
              dataList: ''
            })
          }
        }
    },
    async getTermList () {
        let data = {}
        data.student_id = this.student.id
        let res = requestWrap(await request({
            url:"api/wxapp/student/termList",
            method:'GET',
            data
        }))
        if (res && res.code == 1) {
          if (res.data && res.data.length > 0) {
            this.setData({
                termId: res.data[0].id,
                termName: res.data[0].name,
                termList: res.data
            })
          } else {
            this.setData({
                termList: ''
            })
          }
        }
        this.getCourse()
    }
})